﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OneShoppingDAL;
using OneShoppingBLL;
using OneShoppingUtility;
using OneShoppingModel;

public partial class Backstage_Pages_sysmng_userInfoUpdate : BasePage
{
    private int uid;
    private int roleid;
    private SystemUser systemuser = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (role.RoleId != 1)
        {
            Response.Redirect("login.html");
        }
        if (!IsPostBack)
        {
            BindDDL();
            try
            {
                uid = Convert.ToInt32(Request["id"]);
            }
            catch
            {
                UtilityMessageBox.ShowAndRedirect(this, "获取异常", "userManager.aspx");
                return;
            }
            systemuser = SystemUserBLL.GetInstance().GetEntity(r => r.UserId == uid);
            if (systemuser != null)
            {
                rolelist.SelectedValue = systemuser.RoleId.ToString();
                loginName.Text = systemuser.LoginName;
                password.Attributes["value"] = systemuser.LoginPwd;
                phone.Text = systemuser.UserPhone;
                roleid = systemuser.RoleId;
            }
            else
            {
                UtilityMessageBox.ShowAndRedirect(this, "获取异常", "userManager.aspx");
            }
            Session["uid"] = uid;
        }
        else
        {
            uid = Convert.ToInt32(Session["uid"]);
            systemuser = SystemUserBLL.GetInstance().GetEntity(r => r.UserId == uid);
            roleid = systemuser.RoleId;
        }
    }
    //绑定数据源
    private void BindDDL()
    {
        UtilityDataBind.DropDownListBindData<Role>(ref rolelist,
            RoleBLL.GetInstance().GetList(), "RoleName", "RoleId", "角色");
    }
    //保存按钮
    protected void btnSave_Click(object sender, EventArgs e)
    {
        msg_role.Visible = false;
        msg_psw.Visible = false;
        msg_phone.Visible = false;
        msg_name.Visible = false;
        if (rolelist.SelectedValue == "-1")
        {
            msg_role.InnerText = "请选择角色";
            msg_role.Visible = true;
            return;
        }
/*        if (loginName.Text.Length == 0)
        {
            msg_name.InnerText = "请输入用户名";
            msg_name.Visible = true;
            return;
        }*/
        if (password.Text.Length < 6)
        {
            msg_psw.InnerText = "密码不能小于6位";
            msg_psw.Visible = true;
            return;
        }
        if (phone.Text.Length == 13)
        {
            msg_phone.InnerText = "请输入正确的手机号";
            msg_phone.Visible = true;
            return;
        }
/*        var username = SystemUserBLL.GetInstance().GetEntity(r => r.UserId != uid && r.LoginName == loginName.Text);
        if (username!=null)
        {
            msg_name.InnerText = "用户名重复";
            msg_name.Visible = true;
            return;
        }*/
        systemuser.RoleId =Convert.ToInt32(rolelist.SelectedValue);
        systemuser.LoginName = loginName.Text;
        systemuser.LoginPwd = password.Text;
        systemuser.UserPhone = phone.Text;
        
        var reslut = SystemUserBLL.GetInstance().Update(systemuser);
        if (reslut)
        {
            object o=null;
            if(roleid.ToString()!=rolelist.SelectedValue)
            {
                int flag = 1;
                switch(roleid)
                {
                    case 1:
                        o = CompanyUserBLL.GetInstance().GetEntity(r => r.UserId == uid);
                        var c = CompanyUserBLL.GetInstance().Delete(uid);
                        if(!c)
                        {
                            flag = 0;
                        }
                        break;
                    case 2:
                        o = BusinessUserBLL.GetInstance().GetEntity(r => r.UserId == uid);
                        var b = BusinessUserBLL.GetInstance().Delete(uid);
                        if(!b)
                        {
                            flag = 0;
                        }
                        break;
                    case 3:
                        o = GeneralUserBLL.GetInstance().GetEntity(r => r.UserId == uid);
                        var g = GeneralUserBLL.GetInstance().Delete(uid);
                        if(!g)
                        {
                            flag = 0;
                        }
                        break;
                }
                if(flag==0)
                {
                    systemuser.RoleId = roleid;
                    SystemUserBLL.GetInstance().Update(systemuser);
                    UtilityMessageBox.Show(this, "更改角色失败");
                    return;
                }
                switch (rolelist.SelectedValue)
                {
                    case "1":
                        CompanyUser c = new CompanyUser
                        {
                            UserId = uid
                        };
                        bool rc = CompanyUserBLL.GetInstance().Insert(c);
                        if (!rc)
                        {
                            flag = 0;
                        }
                        break;
                    case "2":
                        BusinessUser b = new BusinessUser
                        {
                            UserId = uid
                        };
                        bool rb = BusinessUserBLL.GetInstance().Insert(b);
                        if (!rb)
                        {
                            flag = 0;
                        }
                        break;
                    case "3":
                        GeneralUser g = new GeneralUser
                        {
                            UserId = uid
                        };
                        bool rg = GeneralUserBLL.GetInstance().Insert(g);
                        if (!rg)
                        {
                            flag = 0;
                        }
                        break;
                }
                if (flag == 0)
                {
                    if(rollback(o))
                    {
                        UtilityMessageBox.Show(this, "更改新角色失败");
                    }
                    else
                    {
                        UtilityMessageBox.Show(this, "数据库回滚失败");
                    }
                    return;
                }
            }
            UtilityMessageBox.ShowAndRedirect(this, "修改成功", "userManager.aspx");
        }
        else
        {
            UtilityMessageBox.ShowAndRedirect(this, "修改失败,未知异常", "userManager.aspx");
        }
    }
    //返回按钮
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("userManager.aspx");
    }
    //数据库回滚
    private bool rollback(object o)
    {
        UtilityMessageBox.Show(this, o.ToString());
        bool result=false;
        switch(roleid)
        {
            case 1:
                result = CompanyUserBLL.GetInstance().Insert((CompanyUser)o);
                break;
            case 2:
                result = BusinessUserBLL.GetInstance().Insert((BusinessUser)o);
                break;
            case 3:
                result = GeneralUserBLL.GetInstance().Insert((GeneralUser)o);
                break;
        }
        return result;
    }
}